// 关注动态详情页
//  MZY_LikeDetailViewController.m
//  FullHouse
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_LikeDetailViewController.h"
#import "NetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "MZY_PIC_commentModel.h"
#import "MZY_COM_picCell.h"
#import "MZY_COM_textCell.h"
#import "MJRefresh.h"
#import "DataBaseHandle.h"
#import "MZY_PictureModel.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@interface MZY_LikeDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *informationArr;
@property(nonatomic, retain)UILabel *addCountLabel;
@property(nonatomic, retain)UILabel *commmentCountLabel;

@end

@implementation MZY_LikeDetailViewController

- (void)dealloc
{
    [self.addCountLabel release];
    [self.commmentCountLabel release];
    [self.tableView release];
    [self.informationArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createView];
}

#pragma mark 获取数据
- (void)createData{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.meilijia.com/app/photo_info_more?photo_id=%@",self.idStr];
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.informationArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"photo_col_history"][@"list"]) {
            MZY_PIC_commentModel *pic = [[MZY_PIC_commentModel alloc]init];
            [pic setValuesForKeysWithDictionary:temp];
            [self.informationArr addObject:pic];
            [pic release];
        }
        [self.tableView reloadData];
    }];
}

#pragma mark 创建界面
- (void)createView{
    self.view.backgroundColor = [UIColor cyanColor];
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    // 创建头视图
    UIView *headView= [[UIView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, 500)];
    headView.backgroundColor = [UIColor whiteColor];
    UIImageView *picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, 450)];
    [headView addSubview:picImageView];
    [picImageView sd_setImageWithURL:[NSURL URLWithString:self.picStr]];
    [picImageView release];
    UIImageView *addImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 470, 30, 30)];
    addImageView.image = [UIImage imageNamed:@"symbol09.png"];
    [headView addSubview:addImageView];
    [addImageView release];
    self.addCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 475, 40, 20)];
    self.addCountLabel.text = self.addCount;
    self.addCountLabel.textColor = [UIColor lightGrayColor];
    [headView addSubview:self.addCountLabel];
    [self.addCountLabel release];
    UIImageView *commentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 470, 30, 30)];
    commentImageView.image = [UIImage imageNamed:@"symbol08.png"];
    [headView addSubview:commentImageView];
    [commentImageView release];
    self.commmentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 475, 50, 20)];
    self.commmentCountLabel.text = self.commentCount;
    self.commmentCountLabel.textColor = [UIColor lightGrayColor];
    [headView addSubview:self.commmentCountLabel];
    [self.commmentCountLabel release];
    
    // 创建"tableView"
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = headView;
    [self.tableView registerClass:[MZY_COM_picCell class] forCellReuseIdentifier:@"pic"];
    [self.tableView registerClass:[MZY_COM_textCell class] forCellReuseIdentifier:@"text"];
    [self.tableView release];
    [headView release];
    
    // 下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(refreshView)];
    self.tableView.headerRefreshingText = @"松开即可刷新";
}

#pragma mark 执行刷新
- (void)refreshView{
    [self createData];
    [self.tableView reloadData];
    [self.tableView headerEndRefreshing];
}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.informationArr.count;
}

#pragma mark 设置"tableView"里有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70 * HORIZONTAL;
}

#pragma mark 设置分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

#pragma mark 设置"cell"之间"headerView"的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

#pragma mark 设置"headerView"颜色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor lightGrayColor];
    headerView.alpha = 0.5;
    return headerView;
}


#pragma mark "tableView"的第一个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        MZY_COM_textCell *cell = [tableView dequeueReusableCellWithIdentifier:@"text" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_PIC_commentModel *pic = self.informationArr[indexPath.row];
        [cell.faceImageView sd_setImageWithURL:[NSURL URLWithString:pic.author_face]];
        cell.faceImageView.layer.cornerRadius = 25 * HORIZONTAL;
        cell.faceImageView.layer.masksToBounds = YES;
        cell.nameLabel.text = [NSString stringWithFormat:@"%@%@%@",pic.author_nick,@" ",@"收入画册"];
        cell.nameLabel.textColor = [UIColor lightGrayColor];
        cell.likeLabel.text = pic.col_name;
        cell.likeLabel.textColor = [UIColor orangeColor];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:pic.col_cover]];
        cell.rightImageView.image = [UIImage imageNamed:@"iconfont-later.png"];
        return cell;
}

#pragma mark 返回按钮的点击方法
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
